<?php
//模拟登录
$_SESSION['openids'] = 'ocwfIwI_h9g2Wmm6uhbECfLcQ-dg';

$no_array=array('/mobile/register.php');
if(strpos( $_SERVER['HTTP_USER_AGENT'],'MicroMessenger') > 0 && !in_array($_SERVER['PHP_SELF'],$no_array))
{
    if(!in_array($_SERVER['PHP_SELF'],$no_array))
    {
        require('oauth2.php');
        if (empty($_SESSION['openids'])) 
        {
            $uri = 'http://ytt.tophp.cn/mobile/';
            /*拿到用户的OPENID，获取用户的微信数据并保存在SESSION中*/
            path_weixin($uri);
            die();
        }else{
           //判断是不是用户
            $is_users = $db->getOne("SELECT COUNT(*) FROM wx_users WHERE wecha_id = '{$_SESSION['openids']}'");
           //如果不是用户就添加帐户
           if (!$is_users) 
           {
                //添加用户
                $openid = $_SESSION['wei_info']->openid;
                $nickname = $_SESSION['wei_info']->nickname;
                $sex = $_SESSION['wei_info']->sex;
                $headimgurl = $_SESSION['wei_info']->headimgurl;
                $GLOBALS['db']->query(' INSERT INTO '.$GLOBALS['ecs']->table('users').' ( user_name, wecha_id, head_img) VALUES ( "'.$nickname.'", "'.$openid.'", "'.$headimgurl.'" ) ');
           }
        }
    }
}

function getRandomString($len, $chars=null)
{
    if (is_null($chars)){
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    }
    mt_srand(10000000*(double)microtime());
    for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
        $str .= $chars[mt_rand(0, $lc)];  
    }
    return $str;
}

